"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能测试
Case Name   : 创建unlogged表checkpoint后重启数据库,查看数据是否丢失
Create At   : 2022/7/4
@zou_jialiang0501162244
Description :
    1.创建unlogged表并插入1条数据
    2.设置checkpoint，查询表数据
    3.重启数据库
    4.查询表数据
    5.清理环境
Expect      :
    1.创建unlogged表并插入1条数据成功
    2.设置成功，查询1条数据
    3.重启数据库成功
    4.查询表成功，1条数据
    5.清理环境成功
History     :
"""


import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Table(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Con = Constant()
        self.table = 't_table_test_0285'

    def test_unlogged_table_checkpoint(self):
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        step = '-----step1:创建unlogged表并插入1条数据; expect:成功-----'
        self.logger.info(step)
        res = self.primary_sh.execut_db_sql(
            f"drop table if exists {self.table};"
            f"create unlogged table {self.table}(c_int int);"
            f"insert into {self.table} values(2);")
        self.logger.info(res)
        self.assertIn(self.Con.CREATE_TABLE_SUCCESS, res, "执行失败" + step)
        self.assertIn(self.Con.INSERT_SUCCESS_MSG, res, "执行失败" + step)

        step = '-----step2:设置checkpoint,查询表数据; \
                   expect:设置成功,1条数据-----'
        self.logger.info(step)
        res = self.primary_sh.execut_db_sql(f"checkpoint;"
                                            f"select * from {self.table};")
        self.logger.info(res)
        self.assertIn(self.Con.CHECKPOINT_SUCCESS_MSG, res, "执行失败" + step)
        self.assertEqual(int(res.splitlines()[3].strip()), 2, "执行失败" + step)

        step = '-----step3:重启数据库；expect:成功-----'
        self.logger.info(step)
        res = self.primary_sh.restart_db_cluster()
        self.logger.info(res)
        self.assertTrue(res, "执行失败" + step)

        step = '-----step4:查询unlogged表数据；expect:1条数据-----'
        self.logger.info(step)
        res = self.primary_sh.execut_db_sql(f"select * from {self.table};")
        self.logger.info(res)
        self.assertEqual(int(res.splitlines()[2].strip()), 2, "执行失败" + step)

    def tearDown(self):
        step = '-----step5:清理环境；expect:成功-----'
        self.logger.info(step)
        res = self.primary_sh.execut_db_sql(f"drop table {self.table};")
        self.logger.info(res)
        self.assertIn(self.Con.DROP_TABLE_SUCCESS, res, "执行失败" + step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
